package com.ghh.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ghh.constant.MessageConstant;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.entity.Result;
import com.ghh.pojo.CheckItem;
import com.ghh.pojo.Member;
import com.ghh.service.CheckitemService;
import com.ghh.service.MemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;


    //添加会员
    @PreAuthorize("hasAuthority('MEMBER_ADD')")
    @RequestMapping("/addMember")
    public Result addMember(@RequestBody Member member){
        try {
            memberService.add(member);

            //添加成功
            return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    //分页查询会员
    @PreAuthorize("hasAuthority('MEMBER_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return memberService.pageQuery(queryPageBean);
    }

    //删除会员
    @PreAuthorize("hasAuthority('MEMBER_DELETE')")
    @RequestMapping("/deleteMember")
    public Result deleteMember(int id){
        try {
            memberService.deleteMember(id);
            //删除成功
            return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    //修改会员
    @PreAuthorize("hasAuthority('MEMBER_EDIT')")
    @RequestMapping("/updateMember")
    public Result updateMember(@RequestBody Member member){
        try {
            memberService.updateMember(member);
            //修改成功
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //修改失败
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    //根据ID查找会员
    @PreAuthorize("hasAuthority('MEMBER_QUERY')")
    @RequestMapping("/findById")
    public Result findById(int id){
        Member member = null ;
        try {
             member = memberService.findById(id);
            //查询成功
            return new Result(true,MessageConstant.QUERY_MEMBER_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    //查询所有会员
    @PreAuthorize("hasAuthority('MEMBER_QUERY')")
    @RequestMapping("/findAllMember")
    public Result findAllMember(){

        try {
            List<Member> list = memberService.findAll();
            return new Result(true,MessageConstant.QUERY_MEMBER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }
}
